package com.huluobo.a2205monthdemo.repair

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.EncryptUtils
import com.blankj.utilcode.util.SPUtils
import com.google.gson.Gson
import com.huluobo.a2205monthdemo.R
import com.huluobo.a2205monthdemo.adapter.RepairAdapter
import com.huluobo.a2205monthdemo.adapter.RepairUserAdapter
import com.huluobo.a2205monthdemo.databinding.ActivityRepairBinding
import com.huluobo.base.bean.RepairBean
import com.huluobo.base.bean.RepairBeanItem
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import java.lang.StringBuilder

@AndroidEntryPoint
class RepairActivity : BaseActivity<ActivityRepairBinding, RepairViewModel>() {
    private lateinit var repairAdapter: RepairAdapter
    private lateinit var repairUserAdapter: RepairUserAdapter
    private var isSelectAll: Boolean = false
    private lateinit var repairBeanItem: RepairBeanItem
    override fun initData() {
        vm.getAllRepair()
        vm.getAllRepairSuccess.observe(this) {
            val list = Gson().fromJson(it, RepairBean::class.java)
            repairAdapter.data.clear()
            repairAdapter.data.addAll(list)
            repairAdapter.notifyDataSetChanged()
        }

        vm.verifyRepairSuccess.observe(this) {
            Log.i("TagA", "verifyRepair:$it")
        }
    }

    override fun initView() {
        binding.repairHeader.headerTitle.text = "维修管理"
        binding.repairHeader.headerBack.visibility = View.VISIBLE
        binding.repairHeader.headerBack.setOnClickListener { finish() }

        binding.repairHeader.headerEdit.visibility = View.VISIBLE
        binding.repairHeader.headerEdit.setOnClickListener {
            if (binding.repairHeader.headerEdit.text.equals("编辑")) {
                binding.repairHeader.headerEdit.text = "完成"
                showSelect(true)
                binding.repairUserCl.visibility = View.VISIBLE
                repairUserAdapter.data.addAll(arrayListOf("张师傅", "李师傅", "叶师傅", "喵师傅"))
                repairUserAdapter.notifyDataSetChanged()
            } else {
                binding.repairHeader.headerEdit.text = "编辑"
                showSelect(false)
                binding.repairUserCl.visibility = View.GONE
                repairUserAdapter.data.clear()
            }
        }
        binding.repairRv.layoutManager = LinearLayoutManager(this)
        repairAdapter = RepairAdapter()
        binding.repairRv.adapter = repairAdapter

        repairAdapter.setOnItemClickListener { adapter, view, position ->
            if (repairAdapter.data[position].isShow) {
                repairAdapter.data[position].isSelect = !repairAdapter.data[position].isSelect
                repairAdapter.notifyDataSetChanged()
                repairBeanItem = repairAdapter.data[position]
            }
        }

        binding.repairUserRv.layoutManager = LinearLayoutManager(this)
        repairUserAdapter = RepairUserAdapter()
        binding.repairUserRv.adapter = repairUserAdapter

        repairUserAdapter.setOnItemClickListener { adapter, view, position ->
            repairUserAdapter.getViewByPosition(position, R.id.item_repair_user_tv)
                ?.setBackgroundResource(R.drawable.shape_repair_user)
        }

        binding.repairSelectAllIv.setOnClickListener {
            if (isSelectAll) {
                isSelectAll = false
                binding.repairSelectAllIv.setImageResource(R.drawable.gou_default)
                isSelectAll(isSelectAll)
            } else {
                isSelectAll = true
                binding.repairSelectAllIv.setImageResource(R.drawable.gou)
                isSelectAll(isSelectAll)
            }
        }

        binding.repairCommitBtn.setOnClickListener {
            //    {
//        "repairId": 0,
//        "sign": "string",
//        "time": "string",
//        "userId": 0,
//        "verify": 0
//    }
            val map = mutableMapOf<String, Any>()
            map[Const.PARAM_REPAIR_ID] = repairBeanItem.repairId
            map[Const.PARAM_VERIFY] = 1
            map[Const.PARAM_USER_ID] = SPUtils.getInstance().getInt(Const.PARAM_UID)
            map[Const.PARAM_TIME] = (System.currentTimeMillis() / 1000).toString()

            val sb = StringBuilder("tamboo")
            sb.append(repairBeanItem.repairId)
                .append(1)
                .append(SPUtils.getInstance().getInt(Const.PARAM_UID))
                .append((System.currentTimeMillis() / 1000).toString())
            val sign = EncryptUtils.encryptMD5ToString(sb.toString()).toLowerCase()
            map[Const.PARAM_SIGN] = sign

            val json = Gson().toJson(map)
            val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
            vm.verifyRepair(body)
        }

    }

    private fun isSelectAll(isSelectAll: Boolean) {
        repairAdapter.data.forEach {
            it.isSelect = isSelectAll
        }
        repairAdapter.notifyDataSetChanged()
    }

    private fun showSelect(isShow: Boolean) {
        repairAdapter.data.forEach {
            it.isShow = isShow
        }
        repairAdapter.notifyDataSetChanged()
    }
}